package com.qys.livesMall.navy.config;

import lombok.Data;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @author kenny
 */
//@Configuration
//@ConfigurationProperties(prefix = "http.proxy")
@Data
public class HttpProxyConfig {

    private String ip="afliplc.getxlx.com";

    private Integer port =59911;

    private String type="https";

    private String userName="";

    private String password="";

    /**
     * 是否启用
     */
    private Boolean isActive =true;

    public CloseableHttpClient createProxyHttpClient(){
        //代理地址、端口号、协议
        //添加代理
        HttpHost httpHost=new HttpHost(ip, port, type);
        BasicCredentialsProvider provider = new BasicCredentialsProvider();
        provider.setCredentials(new AuthScope(httpHost),new UsernamePasswordCredentials(userName,password));
        //设置账号密码
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
        return httpclient;
    }

    public HttpHost getHttpHost(){
        return new HttpHost(ip, port, type);
    }

}

class test{

    public static void main(String[] args) throws IOException {
        HttpGet httpget = new HttpGet("https://api.binance.com/api/v3/exchangeInfo");

        HttpProxyConfig httpProxyConfig=new HttpProxyConfig();
        //是否代理
        if (httpProxyConfig != null && httpProxyConfig.getIsActive()) {
            //走代理
            CloseableHttpClient httpclient = httpProxyConfig.createProxyHttpClient();
            RequestConfig config = RequestConfig.custom().setProxy(httpProxyConfig.getHttpHost()).build();
            httpget.setConfig(config);
            CloseableHttpResponse response = httpclient.execute(httpget);
            System.out.println(response.getEntity());
        }


        //HttpGet httpget =new HttpGet("https://maps.google.com/maps/api/geocode/json?address=UAEDubai0DubaiiMile%20Delivery%20LLC,%20Shed%202,%20Near%20Pepsi-Co%20roundabout,%20Dubai%20Investment%20Park%20-%202%20-%20Dubai%20-%20%E9%98%BF%E6%8B%89%E4%BC%AF%E8%81%94%E5%90%88%E9%85%8B%E9%95%BF%E5%9B%BD&sensor=false&key=AIzaSyBibsZzBZSQV4M2T10ISy35SMDxp4Jm9tE");

        // 执行get请求.


    }
}
